package com.young.shop_admin.dao.impl;

import com.young.shop_admin.dao.SellerDao;
import com.young.shop_admin.pojo.entity.Seller;
import com.young.shop_admin.utils.DBUtils;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class SellerDaoImpl implements SellerDao {
    //创建自动化管理工具
    private static QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

    //开启驼峰映射
    private static BeanProcessor bean = new GenerousBeanProcessor();
    private static RowProcessor processor = new BasicRowProcessor(bean);


    @Override
    public Seller getSellerByUserName(String name) {
        //执行的SQL语句
        String sql = "SELECT * FROM a_user Where user_name = ? ";
        //定义返回形式
        BeanHandler<Seller> sellerBeanHandler = new BeanHandler<>(Seller.class, processor);
        //执行sql
        try {
            return queryRunner.query(sql,sellerBeanHandler,name);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int getAddUserNumber(String beforeDate, String nowDate) {
        //执行的sql语句
        String sql = "select count(*) from t_user WHERE register_time BETWEEN ? AND ? ";
        Object[] params = {beforeDate,nowDate};
        Long countL = 0l;
        try {
            countL =  (Long)queryRunner.query(sql, new ScalarHandler<>(),params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return countL.intValue();
    }
}
